import request from 'src/third-utils/request/index.js';
Page({
	data: {
		openId: '',
		nickname: '',
		curId: '',
		categroyList:[],
		reportDetail: {},
		content: {},
		babyInfo: {}
	},
	onLoad(e) {
		this.setData({
			curId: e.id
		})
		
		
		request({
			path: 'api/recordType/findList',
			method: 'post',
			data:{
				level:1
			},
			succeed: (res) => {
				console.log(res, 122)
				if (res.code == 200) {
					this.setData({
						categroyList: res.data,
					})
				}
			},
			failed: (err) => {
				console.log('err:', err);
			}
		})
	},
	onShow() {


		console.log(getApp().globalData, 22222)
		console.log(getApp(), 22222)
		const {
			app
		} = getApp().getYouZanYunSdk();
		app
			.getUserInfo()
			.then(({
				userInfo,
				state
			}) => {
				console.log(userInfo, state, 1111)

				this.setData({
					openId: userInfo.openId,
					nickname: userInfo.nickname
				})
				this.getList()
			})
			.catch((error) => {});
	},
	
	
	  toEventList(){
	    wx.navigateTo({
	      url: '/pages/event-report/index?babyId='+this.data.curId
	    })
	  },
	  toEventAdd(e){
	    let idx=e.currentTarget.dataset.index
	    let route=[
	      {
	        title:'消化道',
	        path:'/pages/eventxhd/index?cateId='
	      }, {
	        title:'皮肤',
	        path:'/pages/event-pf/index?cateId='
	      }, {
	        title:'呼吸道',
	        path:'/pages/eventhxd/index?cateId='
	      }, {
	        title:'医疗',
	        path:'/pages/eventyl/index?cateId='
	      },{
	        title:'喂奶',
	        path:'/pages/eventwn/index?cateId='
	      },{
	        title:'过敏原',
	        path:'/pages/eventgmy/index?cateId='
	      },{
	        title:'辅食',
	        path:'/pages/eventfs/index?cateId='
	      },{
	        title:'用药',
	        path:'/pages/eventmedication/index?cateId='
	      },
	    ]
	    route.some(item=>{
	      if(item.title==this.data.categroyList[idx].name){
	
	        wx.navigateTo({
	          url: item.path+this.data.categroyList[idx].id+'&babyId='+this.data.curId
	        })
	      }
	    })
	  },
	getList() {
		request({
			path: 'api/baby/findPage',
			method: 'POST',
			data: {
				pageSize: 20,
				userId: this.data.openId
			},
			succeed: (res) => {

				console.log(res, 122)
				let info = res
				if (info.code == 200) {

					this.setData({
						babyList: info.data.list
					})

					if (this.data.curId) {
						this.getInfo()
					} else {
						if (this.data.babyList.length <= 0) {
							wx.navigateTo({
								url: '/pages/add/index',
							})
						} else {
							this.setData({
								curId: this.data.babyList[0].id
							})
							this.getInfo()
						}

					}
				}
			},
			failed: (err) => {
				console.log('err:', err);
			}
		})
	},
	getInfo() {

		request({
			path: 'api/baby/view',
			method: 'get',
			data: {
				id: this.data.curId
			},
			succeed: (res) => {

				console.log(res, 122)
				if (res.code == 200) {
					this.setData({
						babyInfo: res.data,
					})
				}
			},
			failed: (err) => {
				console.log('err:', err);
			}
		})
		this.setData({
			reportDetail: {},
			content: {}
		})
		request({
			path: 'api/allergyReport/findPage',
			method: 'POST',
			data: {
				pageSize: 2,
				babyId: this.data.curId,
			},
			succeed: (res) => {

				console.log(res, 122)
				if (res.code == 200) {
					if (res.data.list.length > 0) {

						this.setData({
							reportDetail: res.data.list[0],
							content: JSON.parse(res.data.list[0].content)
						})
					}
				}
			},
			failed: (err) => {
				console.log('err:', err);
			}
		})
	},
	selectBaby(e) {
		this.setData({
			curId: e.currentTarget.dataset.id
		})
		this.getInfo()
	},
	addBaby() {
		wx.navigateTo({
			url: '/pages/add/index',
		})
	},
	toCe() {

		wx.navigateTo({
			url: '/pages/index/index',
		})
	},
	toBao() {

		wx.navigateTo({
			url: '/pages/reportlist/index',
		})
	},
	toKefu() {
		wx.navigateTo({
			url: '/pages/event-report/index',
		})
		// const sdk = getApp().getYouZanYunSdk();
		// sdk
		// 	.openWebview("https://shop121239703.youzan.com/v3/im/index?kdt_id=112185116", "navigate", {
		// 		youzan: true
		// 	})
		// 	.then(() => success())
		// 	.catch(() => fail());
	},
	toAdd() {
		wx.navigateTo({
			url: '/pages/add/index?id=' + this.data.curId,
		})
	},
	toPage: function(e) {
		wx.navigateTo({
			url: '/pages/index/index',
		})
	},
});
